package com.aabte.lota.auth;

import com.baomidou.mybatisplus.annotation.Version;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/3/29
 */
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {

  /** 分页插件 */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }

  /**
   * 乐观锁插件 #主要适用场景 当要更新一条记录的时候，希望这条记录没有被别人更新
   *
   * <p>乐观锁实现方式：
   *
   * <p>取出记录时，获取当前version 更新时，带上这个version 执行更新时， set version = newVersion where version = oldVersion
   * 如果version不对，就更新失败
   *
   * <p>实体字段需要加注解 {@link Version}
   *
   * <p>支持的数据类型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
   *
   * <p>示例SQL原理: update tbl_user set name = 'update',version = 3 where id = 100 and version = 2
   */
  @Bean
  public OptimisticLockerInterceptor optimisticLockerInterceptor() {
    return new OptimisticLockerInterceptor();
  }
}
